home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / libraries / supralib.lha / SupraLib / source / FCopy.c next >
Encoding:
C/C++ Source or Header  |  1995-03-08  |  3.8 KB  |  131 lines

  1. /****** FCopy **************************************************************
  2. *
  3. *   NAME
  4. *       FCopy -- copies source file to destination file (V10)
  5. *       (dos V36)
  6. *
  7. *   SYNOPSIS
  8. *       error = FCopy(source, dest, buffer)
  9. *
  10. *       UBYTE = FCopy(char *, char *, LONG);
  11. *
  12. *   FUNCTION
  13. *       This function works very similar to C:Copy program. It copies
  14. *       a source file to a destination file.
  15. *
  16. *   INPUTS
  17. *       source - pointer to a source file name (with a relative or
  18. *                absolute path)
  19. *       dest - pointer to a destination file name
  20. *       buffer - maximum size of a buffer (in bytes) to be
  21. *                allocated for copying. If this buffer is 0, FCopy()
  22. *                will try to allocate buffer a size of a source file,
  23. *                or the largest memory block available. (this is the
  24. *                fastest way).
  25. *
  26. *   RESULT
  27. *       error - zero if no error. Function may return one of the
  28. *       following error definitions:
  29. *
  30. *           FC_ERR_EXIST - Source file does not exist
  31. *           FC_ERR_EXAM  - Error during examination of a source file
  32. *           FC_ERR_MEM   - Not enough memory availabe
  33. *           FC_ERR_OPEN  - Source file could not be oppened
  34. *           FC_ERR_READ  - Error while reading a source file
  35. *           FC_ERR_DIR   - Source file path is a directory
  36. *           FC_ERR_DEST  - Destination file could not be created
  37. *           FC_ERR_WRITE - Error while writing to a destination file
  38. *
  39. *   EXAMPLE
  40. *
  41. *       \* This example will copy a file c:dir to ram: with a new name
  42. *        * list.
  43. *        *\
  44. *
  45. *       UBYTE err;
  46. *
  47. *       if ((err = FCopy("C:Dir", "ram:list", 0)) == 0) {
  48. *
  49. *           no errors...
  50. *
  51. *       } else {
  52. *           printf("Error: %d\n", err); \* Error occured during FCopy() *\
  53. *
  54. *       }
  55. *
  56. *   NOTES
  57. *       If an error occurs then a destination file will not be deleted
  58. *       if it has already been partly copied.
  59. *
  60. ************************************************************************/
  61.  
  62. #include <dos/dos.h>
  63. #include <exec/memory.h>
  64. #include <clib/dos_protos.h>
  65. #include <clib/exec_protos.h>
  66. #include <libraries/supra.h>
  67.  
  68. extern struct Library *DOSBase;
  69.  
  70.  
  71. UBYTE FCopy(char *source, char *dest, LONG buf)
  72. {
  73. struct FileInfoBlock fib;
  74.  
  75. LONG fsize, max;  /* part = One part of buffer */
  76. APTR mem=NULL;
  77. LONG len;
  78. BPTR lock,fsource=NULL,fdest=NULL;
  79. UBYTE err=0;
  80.  
  81.     if ((lock = Lock(source, ACCESS_READ)) == NULL) return(FC_ERR_EXIST);
  82.     if (Examine(lock, &fib) == NULL) {
  83.         UnLock(lock);
  84.         return(FC_ERR_EXAM);
  85.     }
  86.  
  87.     if (fib.fib_DirEntryType < 0) {
  88.         fsize = fib.fib_Size;
  89.  
  90.         if (fdest = Open(dest, MODE_NEWFILE)) {
  91.  
  92.             if (buf == 0) max = AvailMem(MEMF_LARGEST);
  93.             else max = buf;
  94.  
  95.             if (max > fsize) max = fsize;
  96.  
  97.             /* Allocate a buffer */
  98.             while ((mem = AllocMem(max, 0L)) == NULL && max > 1024) max-=1024;
  99.  
  100.             if (mem) {
  101.  
  102.               if (fsource = OpenFromLock(lock)) {
  103.                   /* Do the copying process */
  104.  
  105.                   do {
  106.                       len = Read(fsource, mem, max);
  107.                       if (len == -1) {
  108.                           err = FC_ERR_READ;
  109.                           break;
  110.                       } else if (len == 0) break;
  111.  
  112.                        if ((LONG) Write(fdest, mem, len) != len) {
  113.                           err = FC_ERR_WRITE;
  114.                           break;
  115.                       }
  116.                   } while (TRUE);
  117.               } else err = FC_ERR_OPEN; /* if OpenFromLock */
  118.             } else err = FC_ERR_MEM; /* Not enough memory */
  119.         } else err = FC_ERR_DEST;   /* if Destination File Open */
  120.     } else err = FC_ERR_DIR;    /* If source is dir */
  121.  
  122.     if (fsource) Close(fsource);
  123.     if (fdest) Close(fdest);
  124.     if (lock) UnLock(lock);
  125.     if (mem) FreeMem(mem, max);
  126.  
  127.     return(err);
  128. }
  129.  
  130.  
  131.